import libtcodpy as libtcod
import ent, events

class ItemAction(object):
	def __init__(self, owner, name):
		self.owner = owner
		self.name = name
		
	def act(self, caller = None):
		pass

class Item(object):
	def __init__(self, name = "undefined"):
		self.owner = None
		self.effects = {}
		
		self.slot = None	
		self.name = name
		
		self.use_action = None
		pass
		
	#force target entity to put this item into own container
	def pick_up(self, _ent):
		if isinstance(_ent, ent.Entity):
			if hasattr(_ent, "container"):
				
				if self.owner and hasattr(self.owner,"remove"):
					self.owner.remove(self)
				
				_ent.container.append(self)
				self.owner = _ent.container
				
	def get_effect(self,key):
		if key in self.effects:
			return self.effects[key]
		else:
			return None

	#make item owner drop it
	def drop_item(self, caller):
		#print "ITEM IS DROPPED"
		
		if not caller.container and not self in caller.container:
			return
		
		x, y = caller.x, caller.y
		#tile = caller.tile
		
		item_ent = ent.ItemEntity(caller.em, x, y, 'i', libtcod.gray)
		item_ent.item = self
		item_ent.name = self.name
		caller.em.services["objects"].append(item_ent)
		item_ent.spawn()
		
		caller.container.remove(self)
		
		
		pass

	def get_action_list(self):	
		class UseAction(ent.EntAction):
			def act(self, caller):
				#self.owner.use_action(caller)
				
				#TODO: implement some sort of 'quantity' or 'disposable' flag to check if remove item or not
				if self.owner.use_action(caller) != 'cancelled':
					caller.container.remove(self.owner)
				
		class DropAction(ent.EntAction):
			def act(self, caller):
				self.owner.drop_item(caller)
				
		a_list = []
		
		if self.owner:
			
			if not self.use_action is None:
				a_list.append(UseAction(self,"Use"))
				
			a_list.append(DropAction(self,"Drop"))

		return a_list
		

	# basic class for food and other shit
	# will remove item on using

class FoodItem(Item):
	def __init__(self, name = "Food"):
		super(FoodItem,self).__init__(name)
			
		self.use_action = self.eat
			
	def eat(self,caller):
		#container = self.owner
		caller.body.heal(5)
		
		caller.body.set_hunger(caller.body.hunger + 50)
		#return 'used'
		
	def get_action_list(self):	
		class EatAction(ent.EntAction):
			def act(self,caller):
				item = self.owner
				
				if not caller.container and not self in caller.container:
					return

				if self.owner.use_action(caller) != 'cancelled':
					caller.container.remove(item)
					
		class DropAction(ent.EntAction):
			def act(self, caller):
				self.owner.drop_item(caller)
					
		a_list = []

		if not self.use_action is None:
			a_list.append(EatAction(self,"Eat"))
					
		a_list.append(DropAction(self,"Drop"))

		return a_list
